#include "bar.h"
#include "ball.h"
#include "highgui.h"
#include <stdio.h>
#include <math.h>

bar::bar()
{
	y = 200; //center y coordinate
	x = 0; //top-left corner coordinate
	width = 10;
	height = 120;
	lives = 10;
	font = cvFont(3);
}

void bar::draw(IplImage *img)
{
	

	if (y-height/2 < 0)
		y = height/2;
	if (y+height/2 > img->height)
		y = img->height-height/2;
	cvRectangleR(img, cvRect(x, y - height /2 , width, height), CV_RGB(0, 255, 0),-1);
}


void bar::checkCollision(ball &B)
{
	
	if (barRegion(x, y - height/2, width, height, B.getX(), B.getY(), B.getR() , B.getR()))
	    B.bounce();
	else
	{
		if (this->x==0){
            if(B.isHitLeft() )
               lives--;
		}
		else{
            if(B.isHitRight())
                lives--;
		}
	}
}

bool bar::barRegion(int barx, int bary, int barwidth, int barheight, int ballx, int bally, int ballwidth, int ballheight)
{
	
	if ((ballx > barx +barwidth + ballwidth   ) ||  (bary > bally + ballheight ) || (bally > bary + barheight) || ( barx > ballx + ballwidth  ) )       
	{
	    return false;
	}
	return true;
	
}


